import type { UserInfo } from '@vben/types';

import { requestClient } from '#/api/request';

export namespace UserApi {
  export interface LoginUser {
    client_id: string;
    client_secret: string;
    grant_type: string;
    username: string;
    password: string;
  }
  export interface RegisterUser {
    username: string;
    password: string;
    email: string;
    institution?: string;
  }
  export interface Email {
    email: string;
  }
  export interface VerifyCodeInfo {
    email: string;
    code: string;
  }
  export interface ResetPasswordInfo {
    email: string;
    password: string;
  }
  export interface updateUserInfo {
    userId: string;
    username: string;
    institution: string;
  }
}
/**
 * 获取用户信息
 */

function register(registerUser: UserApi.RegisterUser) {
  return requestClient.post('/dsc-systemMS/dsc-user/register', registerUser);
}

function login(loginUser: UserApi.LoginUser) {
  return requestClient.post('/dsc-systemMS/dsc-user/login', loginUser);
}

function logout() {
  return requestClient.get('/dsc-systemMS/dsc-user/logout');
}

function sendcode(email: UserApi.Email) {
  return requestClient.post(
    '/dsc-systemMS/dsc-user/resetPassword/sendCode',
    email,
  );
}

function validateCode(info: UserApi.VerifyCodeInfo) {
  return requestClient.post(
    '/dsc-systemMS/dsc-user/resetPassword/validateCode',
    info,
  );
}

function resetPassword(info: UserApi.ResetPasswordInfo, config: any) {
  return requestClient.post(
    '/dsc-systemMS/dsc-user/resetPassword',
    info,
    config,
  );
}

function getUsersByEmailLink(keyWord: string) {
  return requestClient.get(
    `/dsc-systemMS/dsc-user/getUserByEmailLike/${keyWord}`,
  );
}

function activateUser(code: string) {
  return requestClient.get(`/dsc-systemMS/dsc-user/active/${code}`);
}

function updateUserInfo(updateUserInfo: UserApi.updateUserInfo) {
  return requestClient.post(
    '/dsc-systemMS/dsc-user/updateUserInfo',
    updateUserInfo,
  );
}

function updateUserAvatar(updateUserAvatar: FormData, config: any) {
  return requestClient.post(
    '/dsc-systemMS/dsc-user/updateUserAvatar',
    updateUserAvatar,
    config,
  );
}
function getUserInfo(userId: string) {
  return requestClient.get<UserInfo>(
    `dsc-systemMS/dsc-user/getUserInfo/${userId}`,
  );
}

// 获取管理员信息
function getUserInfoByEmail(email: string) {
  return requestClient.get(`dsc-systemMS/dsc-user/getUserInfoByEmail/${email}`);
}

export default {
  register,
  login,
  logout,
  sendcode,
  validateCode,
  resetPassword,
  getUsersByEmailLink,
  activateUser,
  updateUserInfo,
  updateUserAvatar,
  getUserInfo,
  getUserInfoByEmail,
};
